package com.jay.tools.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.zackratos.ultimatebar.UltimateBar;
import com.jay.tools.R;
import com.jay.tools.recycler.decoration.RecyclerViewItemDecoration;
import com.jay.tools.ui.TopBarView;
import com.jay.tools.utils.DimenUtils;
import com.jay.tools.utils.ResUtil;
import com.jay.tools.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.swipeback.SwipeBackFragment;

/**
 * author: tanjianpei
 * date: 2018/4/12 012 14:51
 * <p>
 * description:
 */
public abstract class BaseFragment extends SwipeBackFragment {

    protected String TAG = this.getClass().getSimpleName();

    private Unbinder mBinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setSwipeBackEnable(false);
        initPresenter();
    }

    // 只在MVPBaseFragment中使用一次，子类不允许使用
    protected abstract void initPresenter();

    /**
     * 设置状态栏颜色
     *
     * @param dark true深色，false浅色
     */
    protected void stateBar(boolean dark) {
        UltimateBar.Companion.with(getProxyActivity())
                .statusDark(dark)                  // 状态栏灰色模式(Android 6.0+)，默认 flase
                .applyNavigation(false)              // 应用到导航栏，默认 flase
                .navigationDark(true)              // 导航栏灰色模式(Android 8.0+)，默认 false
                .create()
                .immersionBar(); // 状态栏透明
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        if (getLayout() instanceof Integer) {
            view = inflater.inflate((Integer) getLayout(), container, false);
        } else {
            view = (View) getLayout();
        }
        mBinder = ButterKnife.bind(this, view);
        onBindView(savedInstanceState, view);

        return attachToSwipeBack(view);
    }

    protected abstract Object getLayout();

    protected void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initLazy(savedInstanceState);
        initLazyEvents();
    }

    protected abstract void initLazy(Bundle savedInstanceState);

    protected void initLazyEvents() {
        // 所有的事件监听
    }

    protected void inflateTopBar(TopBarView topBar) {
        inflateTopBar(topBar, "");
    }

    protected void inflateTopBar(TopBarView topBar, String title) {
        topBar.setTitle(title);
        topBar.setOnTopBarClickListener(new TopBarView.OnTopBarClick() {
            @Override
            public void leftClick() {
                pop();
            }
        });
    }

    protected void initRecycler(RecyclerView recyclerView) {
        initRecycler(recyclerView, 0, 0, false, false);
    }

    protected void initRecycler(RecyclerView recyclerView, float d, int color) {
        initRecycler(recyclerView, d, color, false, false);
    }

    protected void initRecycler(RecyclerView recyclerView, float d, int color, boolean first, boolean bottom) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getProxyActivity()));
        if (d == 0)
            return;
        recyclerView.addItemDecoration(new RecyclerViewItemDecoration.Builder(getProxyActivity())
                .thickness(DimenUtils.dp2px(getProxyActivity(), d))
                .firstLineVisible(first)
                .lastLineVisible(bottom)
                .color(ResUtil.getColor(getProxyActivity(), color))
                .create());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mBinder.unbind();
    }

    @SuppressWarnings("unchecked")
    public <T extends BaseFragment> T getParentDelegate() {
        return (T) getParentFragment();
    }

    protected final ProxyActivity getProxyActivity() {
        return (ProxyActivity) _mActivity;
    }

    protected void toast(String msg) {
        ToastUtil.toast(getProxyActivity(), msg);
    }

    protected void toast(int msg) {
        ToastUtil.toast(getProxyActivity(), msg);
    }


}
